//
//  LocViewController.m
//  Dear
//
//  Created by 陈俊俊 on 15/7/19.
//  Copyright (c) 2015年 陈俊俊. All rights reserved.
// 亲爱的位置

#import "LocViewController.h"
#import <BaiduMapAPI/BMapKit.h>

@interface LocViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>
{
    BMKMapView *_mapView;
    BMKPointAnnotation *_pointAnnotation;
    NSString *_address;
}
@end

@implementation LocViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTitleViewWith:@"亲爱的位置"];
    self.view.backgroundColor = RGBA(246, 246, 246, 1);
    //初始化界面
    [self bulidLayout];
    //获取位置
    [self getLoc];
}

#pragma mark - 初始化
- (void)bulidLayout {
    //创建地图视图
    _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width, ScreenSize.height - 64)];
    _mapView.zoomLevel = 19;
    [self.view addSubview:_mapView];
    
}

#pragma mark - 获取位置
- (void)getLoc
{
    //获取亲爱的位置
    NSDictionary *dict = @{@"userId":[DearUser sharedInstance].userId,@"flag":@"0"};
    NSString *url = [ServerUrl stringByAppendingFormat:@"/%@",DearHttpCheckLocInfor];
    [[HttpRequest sharedInstance] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [DearLoadingView hideHUD];
        if (responseObject) {
            NSDictionary *dict = (NSDictionary *)responseObject;
            if ([dict[@"Result"] integerValue] == 0)
            {
                //开始定位
                [self startLocation:dict];
            }else {
                [DearLoadingView showTips:dict[@"Message"]];
            }
        }else{
            [DearLoadingView showTips:@"获取失败"];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [DearLoadingView showTips:error];
    }];

}

#pragma mark  - 开始定位
- (void)startLocation:(NSDictionary *)dict{
    NSDictionary *item = dict[@"item"];
    CLLocationDegrees latitude = [item[@"latitude"] doubleValue];
    CLLocationDegrees longitude = [item[@"longitude"] doubleValue];

    _mapView.centerCoordinate = CLLocationCoordinate2DMake(latitude,longitude);
    BMKCoordinateRegion viewRegion = BMKCoordinateRegionMake(CLLocationCoordinate2DMake(latitude,longitude), BMKCoordinateSpanMake(0.02f,0.02f));
    BMKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];
    [_mapView setRegion:adjustedRegion animated:YES];
    //添加标注
    _address = item[@"address"];
    [self addPointAnnotation:latitude longitude:longitude];
    
}


#pragma mark - 添加标注
//添加标注
- (void)addPointAnnotation:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude
{
    if (_pointAnnotation == nil) {
        _pointAnnotation = [[BMKPointAnnotation alloc]init];
        CLLocationCoordinate2D coor;
        coor.latitude = latitude;
        coor.longitude = longitude;
        _pointAnnotation.coordinate = coor;
        _pointAnnotation.title = @"地点";
        _pointAnnotation.subtitle = _address;
    }
    [_mapView addAnnotation:_pointAnnotation];
}

#pragma mark - 添加标注Delegate
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    //普通annotation
    if (annotation == _pointAnnotation) {
        NSString *AnnotationViewID = @"renameMark";
        BMKPinAnnotationView *annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
        if (annotationView == nil) {
            annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
            // 设置颜色
            annotationView.pinColor = BMKPinAnnotationColorPurple;
            // 从天上掉下效果
            annotationView.animatesDrop = YES;
            // 设置可拖拽
            annotationView.draggable = YES;
            //修改图标
            annotationView.image = [UIImage imageNamed:@"location1"];
        }
        return annotationView;
    }
    return nil;
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
    
}
- (void)dealloc {
    if (_mapView) {
        _mapView = nil;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
